clear;clc;clf; close all;

%% 比较嵌入到树脂中的金属用或不用bool运算先行去除的区别
%% 结论：区别很小，可忽略
%%
mws = CST_Initilization;
tic;
CST_Modeling(mws);
disp(["建模成功，所用时间为：", num2str(toc), 'S']);

%% 建模函数
function [] = CST_Modeling(mws)

    Frq = [0.5, 2]; % 工作频率,太赫兹波波长范围是3mm-30um
    FC_Property_Setup_Simplfied(mws, Frq);
    % 仿真参数设置
    Px = 300; %结构X向边长
    Py = 300; %结构Y向边长
    MetalThick = 0.2; %基地层金属厚度
    DielectThick = 100;
    L1 = 100; W1 = 35; Dx = 0; Sx = Px / 4;
    L2 = 80; W2 = 30; Dy = 0; Sy = Py / 4;

    % 在CST中加入结构参数，方便后续查看
    invoke(mws, 'StoreParameter', 'Px', Px);
    invoke(mws, 'StoreParameter', 'Py', Py);
    invoke(mws, 'StoreParameter', 'MetalThick', MetalThick);
    invoke(mws, 'StoreParameter', 'DielectThick', DielectThick);
    invoke(mws, 'StoreParameterWithDescription', 'L1 ', L1, '长度不能超过周期Py的一半');
    invoke(mws, 'StoreParameterWithDescription', 'L2 ', L2, '长度不能超过周期Py的一半');
    invoke(mws, 'StoreParameter', 'W1 ', W1);
    invoke(mws, 'StoreParameter', 'W2 ', W2);
    invoke(mws, 'StoreParameter', 'Dx ', Dx);
    invoke(mws, 'StoreParameter', 'Dy ', Dy);
    invoke(mws, 'StoreParameter', 'Sx ', Sx);
    invoke(mws, 'StoreParameter', 'Sy ', Sy);
    CST_Post = CST_MicrowaveStudio();

    % 组件
    % FC_New_Component(mws, "Ground");
    FC_New_Component(mws, "介质");
    FC_New_Component(mws, "金属");

    % step 1. 建立底层基底

    % FC_Model_Brick(mws, '-Px / 2', 'Px / 2', '-Py / 2', 'Py / 2', '-MetalThick', '0', 'Cu_YW', '金属', '基底');
    CST_Post.addBrick('-Px / 2', 'Px / 2', '-Py / 2', 'Py / 2', '-MetalThick', '0', '基底', '金属', 'Cu_YW');

    % step 2. 建立树脂

    FC_Model_Brick(mws, '-Px / 2', 'Px / 2', '-Py / 2', 'Py / 2', '0', 'DielectThick', 'HTL_YW', '介质', '树脂块');

    % step 3. 建立金属条

    FC_Model_Brick(mws, '-W1 / 2+Sx', 'W1 / 2+Sx', '-L1/ 2', 'L1 / 2', 'DielectThick/2', 'DielectThick', 'Cu_YW', '金属', '金属条1');
    FC_Model_Brick(mws, '-W2 / 2-Sx', 'W2 / 2-Sx', '-L2/ 2', 'L2 / 2', 'DielectThick/2', 'DielectThick', 'Cu_YW', '金属', '金属条2');

    if true

        % Step 4. bool运算减去金属条
        CST_Post.subtractObject("介质:树脂块", "金属:金属条1");
        CST_Post.subtractObject("介质:树脂块", "金属:金属条2");

        % Step 5. 重建金属条
        FC_Model_Brick(mws, '-W1 / 2+Sx', 'W1 / 2+Sx', '-L1/ 2', 'L1 / 2', 'DielectThick/2', 'DielectThick', 'Cu_YW', '金属', '金属条1');
        FC_Model_Brick(mws, '-W2 / 2-Sx', 'W2 / 2-Sx', '-L2/ 2', 'L2 / 2', 'DielectThick/2', 'DielectThick', 'Cu_YW', '金属', '金属条2');
    end

end

function [] = CST_Parameter_Sweep(mws, Para, From_, To_, StepWidth_)
    % Para = 'Dy'; From_ = 0; To_ = 12; StepWidth_ = 4;
    CMDs = '';
    CMDs = [CMDs 'With ParameterSweep'];
    CMDs = [CMDs 10 '  .DeleteAllSequences'];
    %     CMDs = [CMDs 10 '    .SetSimulationType ("Frequency")'];
    CMDs = [CMDs 10 ' .AddSequence ("Sweep_' Para '")'];
    CMDs = [CMDs 10 ' .AddParameter_Stepwidth ("Sweep_' Para '", "' Para '", ' num2str(From_) ', ' num2str(To_) ', ' num2str(StepWidth_) ')'];
    %     CMDs = [CMDs 10 '    .Start'];
    CMDs = [CMDs 10 'End With']; % CMDs
    invoke(mws, 'AddToHistory', ['定义参数化扫描_' Para], CMDs);

end

%% 调用CSTStudio并初始化
function mws = CST_Initilization()
    path = pwd; % 获取当前m文件夹路径
    cst = actxserver('CSTStudio.application');
    mws = invoke(cst, 'NewMWS');
    % app = invoke(mws, 'GetApplicationName');
    % ver = invoke(mws, 'GetApplicationVersion');
    invoke(mws, 'FileNew');
    invoke(mws, 'DeleteResults'); % 删除之前的结果，防止程序停止运行

end
